const wx2my = require('../../../wx2my');
const Behavior = require('../../../Behavior');
var _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
  return typeof o;
} : function(o) {
  return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
}
var app = getApp();
var util = require("../../utils/util.js");
var base = require('./../../../config.js');
var WxParse = require('./../wxParse/wxParse.js');

Page({
  data: {
    time: "17:00",
    // 房间数
    num: 1,
    see_price: !1,
    red_bag: 0,
    red_bag_id: 0,
    mode1: "success",
    mode2: "clear",
    mode3: "clear",
    // 显示结算模态窗
    pay_mode: !1,
    // 确认按钮
    refrer_to: "确认支付",
    type: 4,
    pay_num: 1,
    imgUrl: wx2my.getStorageSync(['imgUrl1']).data,
    user_name: '',
    user_tel: '',
    yj_cost: 0,
    settlement: '0',
    discount: 1,
    total_price: '',
    // 权益卡显示
    hasI: false,
    noI: '',
    interests_list: [],
    inter_id: '',
    selectInter: false,
    selectInfo: '',
    selectInF: false,
    originPrice: 0,
    noList: 0,
    // 店铺id
    hotel_id: ""
  },
  radioChange: function(e) {
    this.setData({
      inter_id: e.detail.value ? e.detail.value * 1 : ''
    });
    console.log(e.detail.value);
    console.log(this.data.originPrice);

    if (e.detail.value != '') {
      this.setData({
        selectInfo: this.data.interests_list[e.detail.value],
        selectInF: true,
        settlement: (this.data.originPrice * 1 * (this.data.interests_list[e.detail.value].rebate * 1) * 0.1).toFixed(2)
      });
    } else {
      this.setData({
        selectInfo: [],
        selectInF: false,
        settlement: this.data.originPrice
      });
    }
  },

  payInterests() {
    // 开通权益卡
    var that = this;
    wx2my.request({
      url: base.siteroot + "?i=" + base.acid + "&c=entry&a=wxapp&do=AddcardOrder&m=zh_jdgjb&user_id=" + wx2my.getStorageSync(['userInfo']).data.id + '&equitycard_id=' + that.data.noI.id + '&price=' + that.data.noI.cost + '&out_trade_no=' + Date.parse(new Date()),
      // url: base.siteroot + "?i=" + base.acid + "&c=entry&a=wxapp&do=AddcardOrder&m=zh_jdgjb&user_id=1" + '&equitycard_id=' + that.data.noI.id + '&price=' + that.data.noI.cost,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {
        console.log(res);
        app.util.request({
          url: "entry/wxapp/Pay",
          cachetime: "0",
          data: {
            openid: app.OpenId,
            money: that.data.noI.cost,
            order_id: res.data.data.order_id,
            type: 'card'
          },
          success: function(o) {
            console.log(o);
            wx.requestPayment({
              timeStamp: o.data.timeStamp,
              nonceStr: o.data.nonceStr,
              package: o.data.package,
              signType: o.data.signType,
              paySign: o.data.paySign,
              success: function(o) {
                console.log(o);
                wx2my.showToast({
                  title: '支付成功'
                });
                that.setData({
                  selectInter: !that.data.selectInter
                }); // 重新请求旅行卡数据

                wx2my.request({
                  url: base.siteroot + "?i=" + base.acid + "&c=entry&a=wxapp&do=MyCard&m=zh_jdgjb&user_id=" + wx2my.getStorageSync(['userInfo']).data.id + '&card_type=1',
                  header: {
                    'content-type': 'application/json' // 默认值

                  },
                  success: function(res) {
                    console.log(res); // 如果有权益卡

                    if (res.data.data.open != '') {
                      that.setData({
                        hasI: true,
                        interests_list: res.data.data.open
                      });
                    } else {
                      that.setData({
                        noI: res.data.data.notopen[0]
                      });
                      WxParse.wxParse('privilege', 'html', that.data.noI.privilege, that, 5);
                      WxParse.wxParse('introduce', 'html', that.data.noI.introduce, that, 5);
                    }
                  }
                });
              },
              fail: function() {
                wx2my.showToast({
                  title: '支付失败',
                  icon: 'none'
                });
                that.setData({
                  selectInter: !that.data.selectInter
                });
              }
            });
          }
        });
      }
    });
  },

  // 点击了权益卡的立即开卡
  makeSure: function(e) {
    console.log(e.currentTarget.dataset.sign);
    var sign = 0;

    if (e.currentTarget.dataset.sign == 'noList') {
      sign = 0;
    } else {
      sign = 1;
    }

    this.setData({
      selectInter: !this.data.selectInter,
      noList: sign
    });
  },

  // 页面数据初始化
  onLoad: function(option) {
    console.log('load......')
    var t = this; // 查询优惠卡
    wx2my.request({
      url: base.siteroot + "?i=" + base.acid + "&c=entry&a=wxapp&do=MyCard&m=zh_jdgjb&user_id=" + wx2my.getStorageSync(["userInfo"]).data.uid + '&card_type=1',
      // url: base.siteroot + "?i=" + base.acid + '&c=entry&a=wxapp&do=MyCard&m=zh_jdgjb&user_id=1&card_type=1',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {
        console.log('确认订单页。。。。')
        console.log(res); // 如果有权益卡
        console.log(res.data.data.open)
        // res.data.data.open != ''
        if (res.data.data.open != '') {
          console.log('确认订单页1111111')
          t.setData({
            hasI: true,
            interests_list: res.data.data.open
          });
        } else {
          console.log('确认订单页222222')
          t.setData({
            noI: res.data.data.notopen[0]
          });
          // WxParse.wxParse('privilege', 'html', t.data.noI.privilege, t, 5);
          // WxParse.wxParse('introduce', 'html', t.data.noI.introduce, t, 5);
        }
      }
    });
    // 请求结束
    t.setData({
      // color: wx2my.getStorageSync(['platform']).data.color
    })

    wx2my.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#ffffff'
    })
    //上一页面传参过来的
    var e = option.room_id,
      a = option.hotel_id,
      n = wx2my.getStorageSync(['day1']).data,
      s = wx2my.getStorageSync(['day2']).data,
      c = wx2my.getStorageSync(['day']).data;
    t.setData({
      room_id: e,
      hotel_id: a,
      day1: n,
      day2: s,
      day: c,
      coupon: 0,
      condition: 0,
      coupons_id: -1,
      form_d: option.form_d,
      grade: wx2my.getStorageSync(['platform']).open_member,
      system: wx2my.getStorageSync(['platform'])
    });
    var r = 0;
    wx2my.newRequest({
      url: base.siteroot,
      data: {
        c: 'entry',
        a: 'wxapp',
        do: 'GetRoomCost',
        m: 'zh_jdgjb',
        room_id: e,
        start: n,
        end: s
      },
      success: (o) => {
        // console.log('获取什么啊。。。。')
        for (var e in console.log(o), o.data) r += Number(o.data[e].mprice);
        //r:0.01   o.data:[0: {dateday: "05月18日", mprice: "0.01"}]
        t.setData({
          z_price: r,
          price_infos: o.data
        })
        t.refresh()
        t.room_num();
      },
      fail: (err) => {
        console.log(err)
      }
    })

    

  },

  // 天择旅行卡协议
  agreement() {
    let that = this;
    wx2my.setStorageSync('agreement', that.data.noI.agreement);
    wx2my.navigateTo({
      url: './../interests/agreement?'
    });
  },

  userinfo: function(o) {
    var e = this;
    app.getUserInfo(function(o) {
      console.log(o)
      e.setData({
        userInfo: o
      });
    });
  },

  refresh: function(o) {
    var a = this,
      e = a.data.room_id,
      t = a.data.hotel_id;
    2 == a.data.grade ? (a.setData({
      discount: 1
    }), a.data.z_price) : app.getUserInfo(function(o) {
      var e = o.id;
      console.log("会员等级为 " + e);
      var t = o.discount;
      null == t && (t = 10)
      console.log("会员折扣为 " + t)
      a.setData({
        discount: t / 10
      })
      null != a.data.z_price && a.cost();
    });
    var n = wx2my.getStorageSync(['userInfo']).data.uid;

    wx2my.newRequest({
      url: base.siteroot,
      data: {
        c: 'entry',
        a: 'wxapp',
        do: 'MyCoupons',
        m: 'zh_jdgjb',
        user_id: n
      },
      success: (res) => {
        // console.log('酒店名字啊。。。。。。111')
        // console.log(res)
        a.setData({
          coupons: res.data
        });
      },
      fail: (err) => {
        console.log(err)
      }
    })

    
    my.showLoading({
      success: (res) => {
        
      },
    });
    wx2my.newRequest({
      url: base.siteroot,
      data: {
        c: 'entry',
        a: 'wxapp',
        do: 'RoomDetails',
        m: 'zh_jdgjb',
        room_id: e
      },
      success: (res) => {
        my.hideLoading();

        a.setData({
          room: res.data,
          yj_cost: Number(res.data.yj_cost)
        })
        a.cost();
      },
      fail: (err) => {
        console.log(err);
        my.hideLoading();
        my.alert({
          title: err 
        });
      }
    })

   
    wx2my.newRequest({
      url: base.siteroot,
      data: {
        c: 'entry',
        a: 'wxapp',
        do: 'PjDetails',
        m: 'zh_jdgjb',
        seller_id: a.data.hotel_id
      },
      success: (res) => {
        // console.log('酒店名字啊。。。。。。3333')
        // console.log(res)
        a.setData({
          hotel: res.data
        });
      },
      fail: (err) => {
        console.log(err)
      }
    })
   
    wx2my.newRequest({
      url: base.siteroot,
      data: {
        c: 'entry',
        a: 'wxapp',
        do: 'MyHb',
        m: 'zh_jdgjb',
        user_id: n, //628
        page: 1
      },
      success: (res) => {
        // console.log('酒店名字啊。。。。。。4444')
        // console.log(res)
        a.setData({
          my_hb: res.data
        })

      },
      fail: (err) => {
        console.log(err)
      }
    })
    
  },

  // 获取房间数
  room_num: function(o) {
    var n = this,
      e = wx2my.getStorageSync(['day1']).data,
      t = wx2my.getStorageSync(['day2']).data,
      a = n.data.room_id;
    wx2my.newRequest({
      url: base.siteroot,
      data: {
        c: 'entry',
        a: 'wxapp',
        do: 'GetRoomNum',
        m: 'zh_jdgjb',
        room_id: a,
        start: e,
        end: t
      },
      success: (o) => {
        // console.log('获取房间数')
        console.log(o);
        var e = [],
          t = [];
        for (var a in o.data.map(function(o) {
          var e;
          e = Number(o.nums)
          t.push(e);
        }), o.data) o.data[a].nums <= 0 && e.push(o.data[a]);

        t = t.sort(app.sort_num_order)
        // console.log('获取房间数有了吗')
        console.log(t)
        n.setData({
          rooms: t[0]
        })
        0 < e.length && my.confirm({
          title: "提示",
          content: e[0].dateday + "没有房间了",
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          success: function(o) {
            console.log("用户点击确定")
            wx2my.navigateBack({
              delta: 1
            })
            // o.confirm ? (console.log("用户点击确定"), wx2my.navigateBack({
            //   delta: 1
            // })) : o.cancel && (console.log("用户点击取消"), wx2my.navigateBack({
            //   delta: 1
            // }));
          },
          fail: function() {
            console.log("用户点击取消")
            wx2my.navigateBack({
              delta: 1
            })
          }
        });
      },
      fali: (err) => {
        console.log(err)
      }
    })
  },
  // 跳转红包页
  use_res_bag: function(o) {
    wx2my.navigateTo({
      url: "../coupon/red_bag"
    });
  },

  //选择时间
  bindTime: function(o) {
    my.datePicker({
      format: 'HH:mm',
      currentDate: '17:00',
      startDate: '00:00',
      endDate: '23:59',
      success: (res) => {
        this.setData({
          time: res.date
        })
      },
    });
    // this.setData({
    //   time: o.detail.value
    // });
  },
  // 增加房间数
  add_num: function(o) {
    var e = this;
    console.log(e.data.rooms)
    if (e.data.rooms == e.data.num) wx2my.showModal({
      title: "",
      content: "没有这么多房间啦"
    }); else {
      var t = e.data.num + 1;
      e.setData({
        num: t
      })
      e.cost();
    }
  },
  //减少房间数
  reduce_num: function(o) {
    var e = this.data.num - 1;
    1 <= e && (this.setData({
      num: e
    }), this.cost());
  },

  see_price: function(o) {
    console.log(o);
    var e = this.data.see_price;
    1 == e ? this.setData({
      see_price: !1
    }) : this.setData({
      see_price: !0
    });
  },

  mode1: function(o) {
    this.setData({
      mode1: "success",
      mode2: "clear",
      mode3: "clear",
      refrer_to: "确认支付",
      type: 1
    })
    this.refresh();
  },

  mode2: function(o) {
    this.setData({
      mode1: "clear",
      mode2: "success",
      mode3: "clear",
      refrer_to: "确认支付",
      yj_cost: 0,
      type: 3
    })
    this.cost();
  },

  mode3: function(o) {
    var e = this,
      t = e.data.hotel,
      a = e.data.userInfo;
    console.log(a), 2 == t.ye_open ? e.setData({
      refrer_to: "该酒店不支持余额支付"
    }) : Number(a.balance) < e.data.settlement ? wx2my.showModal({
      title: "",
      content: "余额不足,请先去充值"
    }) : (e.setData({
      refrer_to: "确认支付",
      type: 2
    }), e.setData({
      mode1: "clear",
      mode2: "clear",
      yj_cost: 0,
      mode3: "success"
    }), e.cost());
  },

  cost: function(o) {
    var e = this,
      // 几间房
      t = e.data.num,
      a = Number(e.data.yj_cost);
    console.log(a);
    // 几间房的钱
    var n = a * t;
    console.log(n);
    // 房间价格
    var s = e.data.z_price;
    // console.log('ssssss'+s);
    var c = e.data.coupon;
    //  console.log('ccccc'+c);
    var r = Number(e.data.red_bag);
    // console.log('rrrrr'+r);
    // 几间房的总价
    var d = Number(s) * t;
    // console.log('dddd'+d);
    // 折扣=几间房*费率
    var i = d * e.data.discount;
    // console.log(e.data.discount)
    // console.log('iiiii'+i);
    // 折扣的金额为
    var l = d - i;
    // console.log('llllll'+l);
    // 最后结算金额为
    var u = i - c + n - r;
    // console.log('uuuu'+u);
    var p = d - c - r;
    // console.log('pppppp'+p);
    (p = p.toFixed(2)) <= 0 && (p = 0), i = i.toFixed(2), l = l.toFixed(2);
    var g = d;
    (u = u.toFixed(2)) <= 0 && (u = 0 != a ? n : .01)

    d = (d += n).toFixed(2)
    g = g.toFixed(2)
    // console.log("房间总金额为 " + g)
    // console.log("折扣价为 " + i)
    // console.log("折扣的金额为 " + l)
    // console.log("最后结算金额为 " + u)
    // console.log('金额为' + e.data.u)
    e.setData({
      price: d,
      total_price: g,
      settlement: isNaN(u) ? '0.00' : u,
      discount_price: i,
      reduction_price: l,
      dis_cost: p,
      yj_price: n,
      originPrice: g
    });
  },

  // 使用优惠券
  use_coupon: function(o) {
    var e = this.data.discount_price,
      t = this.data.hotel_id;
    wx2my.navigateTo({
      url: "../coupon/store_coupon?money=" + e + "&hotel_id=" + t
    });
  },

  // input保存姓名
  bindinputText: function(e) {
    console.log(e.detail.value);
    this.setData({
      user_name: e.detail.value
    });
  },

  // input保存号码
  bindinputTel: function(e) {
    console.log(e.detail.value);
    this.setData({
      user_tel: e.detail.value
    });
  },
  // 去结算
  pay_mode: function(o) {
    var user_id = wx2my.getStorageSync(['userInfo']).data.uid;
    console.log(this.data.hotel)
    if (this.data.user_name == '') {
      my.alert({
        title: '温馨提醒',
        content: '入住人信息不能为空',
        buttonText: '我知道了',
      })
      return false;
    }
    //号码需做检验
    if (this.data.user_tel == '') {
      my.alert({
        title: '温馨提醒',
        content: '入住人号码不能为空',
        buttonText: '我知道了',
      })
      return false;
    } else {
      if (!(/^1(3|4|5|7|8)\d{9}$/.test(this.data.user_tel))) {
        my.alert({
          title: '温馨提醒',
          content: '手机号码有误，请重填',
          buttonText: '我知道了',
        })
        return false;
      }
    }
    var e = this.data.pay_mode;
    //e: false/true 控制模态窗
    // 1 == e ? this.setData({
    //   pay_mode: !1
    // }) : this.setData({
    //   pay_mode: !0
    // });
    console.log(this.data.hotel);
    console.log(this.data.room);
    var e = this,
      t = e.data.form_d;
    var a = e.data.user_tel, //{tel:'15957111940'}, // e.data.userInfo,
      // n = o.detail.formId,
      s = e.data.hotel,
      c = e.data.room,
      r = '330781199508315913',//o.detail.value.code,
      d = 0.01, //e.data.total_price,
      i = e.data.settlement,
      l = e.data.condition,
      u = 0.01,  //e.data.price,
      p = e.data.user_name, //e.data.user_name,
      g = e.data.user_tel, //o.detail.value.tel,
      m = e.data.time,
      _ = s.id,
      f = c.id,
      y = wx2my.getStorageSync(["userInfo"]).data.uid,
      h = e.data.coupons_id,
      x = s.name,
      w = s.address,
      v = s.coordinates,
      S = e.data.day1,//'2019-5-18',//e.data.day1,
      b = e.data.day2,//'2019-5-19', //e.data.day2,
      D = u,
      I = e.data.num,
      T = c.name,
      q = c.size,
      M = e.DateDiff(b, S),
      j = e.data.settlement,
      z = e.data.yj_cost,
      N = e.data.coupon,
      k = c.logo,
      C = e.data.reduction_price,
      F = e.data.red_bag,
      P = e.data.red_bag_id,
      R = e.data.type;
    // console.log("入住人为 " + p)
    // console.log("联系人电话为 " + g)
    // console.log("房间价格为 " + d)
    // console.log("选择的到店时间为 " + m)
    // console.log("酒店id " + _)
    // console.log("房间id " + f)
    // console.log("用户id " + y)
    // console.log("优惠券id " + h)
    // console.log("酒店名字 " + x)
    // console.log("酒店地址 " + w)
    // console.log("酒店经纬度 " + v)
    // console.log("到店日期 " + S)
    // console.log("离店日期 " + b)
    // console.log("结算总金额 " + D)
    // console.log("房间数量 " + I)
    // console.log("房间类型 " + T)
    // console.log("床型 " + q)
    // console.log("入住天数 " + M)
    // console.log("折扣后的价格 " + j)
    // console.log("押金金额 " + z)
    // console.log("优惠券金额 " + N)
    // console.log("支付方式为 " + R);
    var B = e.data.system;
    var O = e.data.pay_num;
    var data = {
      name: p, //客户姓名
      tel: g,  //电话
      price: d, //单价
      dd_time: m, //到店时间
      seller_id: _, // 酒店id
      room_id: '25',// f,  //房间id
      user_id: y, //用户id
      coupons_id: h, //优惠券id
      seller_name: x, //房间名称
      seller_address: w, //地址
      coordinates: v,  //经纬度
      arrival_time: S, //入住时间
      departure_time: b, //离店时间
      num: I, //数量
      room_type: T, //房间类型
      bed_type: q,
      days: M,
      dis_cost: j, //最后折扣后价格
      yj_cost: z,
      yhq_cost: N, //优惠券价格
      total_cost: d,
      room_logo: k, //房间图片
      yyzk_cost: C,
      hb_id: P,
      hb_cost: F,
      from_id: t, //
      classify: 1,
      type: R,
      code: r,
      equitycard_id: e.data.selectInfo.id ? e.data.selectInfo.id : '',  //权益卡id
      equitycard_cost: e.data.selectInfo.rebate ? e.data.selectInfo.rebate : ''
    }
    // my.navigateTo({
    //   url: './payOrder?hotel=' + JSON.stringify(this.data.hotel) + '&room=' + JSON.stringify(this.data.room) + '&settlement=' + this.data.settlement + '&num=' + this.data.num + '&data=' + JSON.stringify(data) + '&price_infos=' + JSON.stringify(this.data.price_infos)
    // });
    my.navigateTo({
      url: './payOrder?hotel=' + this.data.hotel.name + '&room=' + this.data.room.name + '&logo=' + this.data.room.logo + '&settlement=' + this.data.settlement + '&num=' + this.data.num + '&data=' + JSON.stringify(data) + '&price_infos=' + JSON.stringify(this.data.price_infos)
    });

  },
  DateDiff: function(sDate1, sDate2) {
    //sDate1和sDate2是2006-12-18格式  
    var aDate, oDate1, oDate2, iDays;
    aDate = sDate1.split("-");
    oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]); //转换为12-18-2006格式  

    aDate = sDate2.split("-");
    oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);
    iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24); //把相差的毫秒数转换为天数  

    return iDays;
  },
  onReady: function() { },
  onShow: function() {
    this.userinfo();
    // - 1 != this.data.coupons_id ? this.cost() : "" == this.data.coupons_id && this.cost()
    // 0 != this.data.red_bag && this.cost();
  },
  onHide: function() { },
  onUnload: function() { },
  onPullDownRefresh: function() { },
  onReachBottom: function() { }
});